package queryProccessor;

import java.util.ArrayList;
import java.util.StringTokenizer;

public class Create extends Command {

	String statment;

	ArrayList<String> finalArray;
	private final String[] ideal1 = { "CREATE TABLE", "table_name", "coLumns" };
	boolean result = true;
	String[] columns;
	String[] types;

	public Create(String s) {
		statment = s;
		finalArray = new ArrayList<String>();
		try {
			convertToArray();
			System.out.println(result);
		} catch (Exception e) {
			System.out.println("wrong syntax");
		}

	}

	public void convertToArray() {
		int ct = search("CREATE TABLE ", statment);
		int a = search("(", statment);
		int sc = search(";", statment);
		if (ct != -1 && sc != -1) {
			finalArray.add("CREATE TABLE");
			finalArray.add(statment.substring(ct + 12, a).trim());
			finalArray.add(statment.substring(a, sc).trim());
		}
		table_name = finalArray.get(1);
		divide(finalArray.get(2));
	}

	private void divide(String s) {
		if (s.charAt(0) != '(' || s.charAt(s.length() - 1) != ')') {
			result = false;
		}
		s = s.substring(1, s.length() - 1);
		StringTokenizer str = new StringTokenizer(s, ",");
		columns = new String[str.countTokens()];
		types = new String[str.countTokens()];

		int j = 0;
		while (str.hasMoreTokens() && result == true) {

			String c = str.nextToken().trim();
			int index = search(" ", c);
			if (index != -1) {
				columns[j] = c.substring(0, index).trim();
				types[j] = c.substring(index).trim();

			} else
				result = false;

			j++;
		}

	}

	@Override
	public boolean checkSyntax() {
		// TODO Auto-generated method stub
		return false;
	}

}
